package com.aptemo.webetek.client.widgets.scoreboard;

import java.util.List;
import java.util.MissingResourceException;

import com.aptemo.webetek.client.Webetek;
import com.aptemo.webetek.client.widgets.paging.PagingDisplay;
import com.aptemo.webetek.client.widgets.paging.PagingView;
import com.aptemo.webetek.shared.UserBean;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class TeamStandings
		extends Composite implements TeamStandingsDisplay
{
	private static TeamStandingsUiBinder	uiBinder	= GWT.create(TeamStandingsUiBinder.class);

	interface TeamStandingsUiBinder extends UiBinder<Widget, TeamStandings>
	{
	}

	// @UiField
	// HTMLPanel commandPanel;

	@UiField
	FlexTable	scoreList;

	@UiField
	PagingView	pagingPanel;

	@UiField
	Anchor		earningsLink;

	@UiField
	Anchor		placedLink;

	@UiField
	Anchor		wonLink;

	@UiField
	Label		infoField;

	public TeamStandings()
	{
		initWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	public void renderList(List<UserBean> list)
	{
		scoreList.removeAllRows();
		// commandPanel.setVisible(true);

		if (list.size() == 0)
		{
			pagingPanel.setVisible(false);
			scoreList.setText(0, 0, "No entries...");
			return;
		}
		// selBar.setVisible(true);
		pagingPanel.setVisible(true);

		int i = 0;
		for (UserBean item : list)
		{
			TeamStandingsRow row = new TeamStandingsRow(item);
			// scoreList.setWidget(i, 0, new HTML("" + (i + 1) ));
			scoreList.setWidget(i, 1, row);
			i++;
		}
	}

	@Override
	public Widget getDisplay()
	{
		return this;
	}

	@Override
	public HasClickHandlers getList()
	{
		return scoreList;
	}

	@Override
	public int getClickedRow(ClickEvent event)
	{
		int selectedRow = -1;
		HTMLTable.Cell cell = scoreList.getCellForEvent(event);

		if (cell != null)
		{
			// Suppress clicks if the user is actually selecting the
			// check box
			//
			if (cell.getCellIndex() >= 0)
			{
				selectedRow = cell.getRowIndex();
			}
		}

		return selectedRow;
	}

	@Override
	public HasText getInfoField()
	{
		return infoField;
	}

	@Override
	public PagingDisplay getPaging()
	{
		return pagingPanel;
	}

	@Override
	public HasClickHandlers getEarningsLink()
	{
		return earningsLink;
	}

	@Override
	public HasClickHandlers getPlacedLink()
	{
		return placedLink;
	}

	@Override
	public HasClickHandlers getWonLink()
	{
		return wonLink;
	}

	@Override
	public String translate(String key)
	{
		try
		{
			return Webetek.getConstants().getString(key);
		}
		catch (MissingResourceException e)
		{
			return key;
		}
	}

}
